﻿using DataPersonalization.Utis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataPersonalization
{
    public partial class frMain : Form
    {
        public frMain()
        {
            InitializeComponent();
            Grid_Init();

        }

        private void Form_Load()
        {
            Summary_Load();
            Grid_Load();
        }

        private void Summary_Load()
        {
            MyUtis utis = new MyUtis();
            lbTotal.Text = "Total link: " + utis.GetTotal().ToString("#,###");
            lbSuccess.Text = "Success: " + utis.GetTotalSuccess().ToString("#,###");
            lbFalse.Text = "False: " + utis.GetTotalFalse().ToString("#,###");
        }

        private void Grid_Init()
        {
            #region cột Stt
            DataGridViewColumn colStt = new DataGridViewColumn(); // add a column to the grid
            DataGridViewCell cellStt = new DataGridViewTextBoxCell();

            colStt.CellTemplate = cellStt;

            colStt.HeaderText = "Stt";
            colStt.Name = "Stt";
            colStt.Visible = true;
            colStt.Width = 100;
            colStt.HeaderCell.Style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colStt.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colStt.HeaderCell.Style.Font = new Font(this.Font, FontStyle.Bold);
            colStt.DataPropertyName = "No";

            grid.Columns.Add(colStt);
            #endregion

            #region cột Id
            DataGridViewColumn colId = new DataGridViewColumn(); // add a column to the grid
            DataGridViewCell cellId = new DataGridViewTextBoxCell();
            colId.CellTemplate = cellId;
            colId.HeaderText = "Id";
            colId.Name = "Id";
            colId.Visible = true;
            colId.Width = 100;
            colId.HeaderCell.Style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colId.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colId.HeaderCell.Style.Font = new Font(this.Font, FontStyle.Bold);
            colId.DataPropertyName = "Id";

            grid.Columns.Add(colId);
            #endregion

            #region cột Link
            DataGridViewColumn colLnk = new DataGridViewColumn(); // add a column to the grid
            DataGridViewCell cellLnk = new DataGridViewTextBoxCell();
            colLnk.CellTemplate = cellLnk;
            colLnk.HeaderText = "Link";
            colLnk.Name = "Link";
            colLnk.Visible = true;
            colLnk.Width = 500;
            colLnk.HeaderCell.Style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            colLnk.HeaderCell.Style.Font = new Font(this.Font, FontStyle.Bold);
            colLnk.DataPropertyName = "Link";

            grid.Columns.Add(colLnk);
            #endregion

            #region cột status
            DataGridViewColumn colStatus = new DataGridViewColumn(); // add a column to the grid
            DataGridViewCell cellStatus = new DataGridViewTextBoxCell();
            colStatus.CellTemplate = cellStatus;
            colStatus.HeaderText = "Status";
            colStatus.Name = "Status";
            colStatus.Visible = true;
            colStatus.Width = 100;
            colStatus.HeaderCell.Style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colStatus.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            colStatus.HeaderCell.Style.Font = new Font(this.Font, FontStyle.Bold);
            colStatus.DataPropertyName = "Status";

            grid.Columns.Add(colStatus);
            #endregion

            grid.AutoGenerateColumns = false;
        }

        private void Grid_Load()
        {
            MyUtis utis = new MyUtis();
            List<Links> ls = utis.GetListLinksError();
            
            grid.AutoGenerateColumns = false;
            grid.DataSource = ls;
        }

        private static Thread _thrdCrawler;
        private void btStart_Click(object sender, EventArgs e)
        {
            // xóa hết các file logs
            MyUtis utis = new MyUtis();
            utis.DeleteFileLogTotal();
            utis.DeleteFileLogStatus();

            Form_Load();

            _thrdCrawler = new Thread(new ThreadStart(this.StartCrawler));
            _thrdCrawler.Start();

            lbMesStart.Text = "crawling...";
            btStart.Enabled = false;
            btAbort.Enabled = true;
        }

        private void StartCrawler()
        {
            RedisAccess res = new RedisAccess();
            res.GetLinks();
            lbMesStart.Text = "crawler finished";
            btAbort.Enabled = false;
            btStart.Enabled = true;
        }

        private void btAbort_Click(object sender, EventArgs e)
        {
            if (_thrdCrawler != null || _thrdCrawler.ThreadState != System.Threading.ThreadState.Stopped || _thrdCrawler.IsAlive != false)
            {
                _thrdCrawler.Abort();
            }

            lbMesStart.Text = "click Start to crawler";
            btStart.Enabled = true;
            btAbort.Enabled = false;
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            Form_Load();
        }

        private void timerAuto_Tick(object sender, EventArgs e)
        {
            Form_Load();
        }

        int tick = 3;
        private void timerForButton_Tick(object sender, EventArgs e)
        {
            btAutoRefresh.Text = "Refresh Auto(" + tick + "s)";

            if (tick == 0)
            {
                tick = 3;
            }
            else
            {
                tick = tick - 1;
            }
        }

        private void btStopAutoRefresh_Click(object sender, EventArgs e)
        {
            timerAuto.Stop();
            timerForButton.Stop();

            btAutoRefresh.Enabled = true;
            btStopAutoRefresh.Enabled = false;
            btAutoRefresh.Text = "Refresh Auto(0s)";
        }

        private void btAutoRefresh_Click(object sender, EventArgs e)
        {
            timerAuto.Enabled = true; // bật timer để thống kê số thread đang chạy, khi số thread = 0, time sẽ stop
            timerAuto.Start();

            timerForButton.Enabled = true;
            timerForButton.Start();

            btAutoRefresh.Enabled = false;
            btStopAutoRefresh.Enabled = true;

            Form_Load();
        }

        private void btRefreshSum_Click(object sender, EventArgs e)
        {
            Summary_Load();
        }

        private void btViewDomainError_Click(object sender, EventArgs e)
        {
            frDomainError frm = new frDomainError();
            frm.Show();
        }
    }
}
